namespace Amazon.Checkout.Models
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Amazon.Checkout.Helpers;
    using Orchard.ContentManagement;
    using Settings;

    public class CheckoutByAmazonButtonPart : ContentPart<CheckoutByAmazonButtonPartRecord>
    {
        private IEnumerable<string> possibleMappings;
        private ContentPartHelper partHelper;

        public CheckoutByAmazonButtonPart()
            : base()
        {
            this.partHelper = new ContentPartHelper(this);
        }

        public string ProductDescription
        {
            get { return this.partHelper.GetValueFromContentItem(this.SettingsModel.ProductDescriptionMapping); }
        }

        public decimal ProductPrice
        {
            get
            {
                var mapping = this.SettingsModel.ProductPriceMapping;

                if (string.IsNullOrWhiteSpace(mapping))
                {
                    throw new InvalidOperationException("Invalid mapping for price");
                }

                try
                {
                    return Convert.ToDecimal(this.partHelper.GetValueFromContentItem(mapping));
                }
                catch (Exception)
                {
                    throw new InvalidOperationException("Invalid price value");
                }
            }
        }

        public string ProductSku
        {
            get { return this.partHelper.GetValueFromContentItem(this.SettingsModel.ProductSkuMapping); }
        }

        public string ImageUrl
        {
            get { return this.partHelper.GetValueFromContentItem(this.SettingsModel.ProductImageUrlMapping); }
        }

        public string ProductTitle
        {
            get { return this.partHelper.GetValueFromContentItem(this.SettingsModel.ProductTitleMapping); }
        }

        public string UpsellType
        {
            get { return Record.UpsellType; }
            set { Record.UpsellType = value; }
        }

        public string UpsellingProducts
        {
            get { return Record.UpsellingProducts; }
            set { Record.UpsellingProducts = value; }
        }

        public string InclusionCategories
        {
            get { return Record.InclusionCategories; }
            set { Record.InclusionCategories = value; }
        }

        public string ExclusionCategories
        {
            get { return Record.ExclusionCategories; }
            set { Record.ExclusionCategories = value; }
        }

        public string ShippingMethods
        {
            get { return Record.ShippingMethods; }
            set { Record.ShippingMethods = value; }
        }

        public int? PromotionId
        {
            get { return Record.PromotionId; }
            set { Record.PromotionId = value; }
        }

        public int? TaxId
        {
            get { return Record.TaxId; }
            set { Record.TaxId = value; }
        }

        public bool ExpressCheckout
        {
            get { return this.SettingsModel.ExpressCheckout == null || this.SettingsModel.ExpressCheckout == "true"; }
        }

        private CheckoutByAmazonButtonPartSettings SettingsModel
        {
            get
            {
                var settingsModel = this.Settings.GetModel<CheckoutByAmazonButtonPartSettings>();
                settingsModel.PossibleMappings = this.GetPossibleMappings();
                return settingsModel;
            }
        }

        private IEnumerable<string> GetPossibleMappings()
        {
            if (this.possibleMappings == null)
            {
                this.possibleMappings = this.partHelper.GetPossibleContentTypeMappings();
            }
            
            return this.possibleMappings;
        }
    }
}